package org.wkg.lesson11;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Demo_1102{

    public static void main(String[] args) {
        //一次只能执行5个线程
        Semaphore semaphore =new Semaphore(5);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"申请资源....");
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"资源申请成功=======");
                    TimeUnit.SECONDS.sleep(1);
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName()+"销毁******");

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        for (int i = 0; i < 20; i++) {
            Thread thread=new Thread(runnable);
            thread.start();
        }
    }
}
